package com.lysj.admin.master.bussiness.company.recharge.service;

import com.lysj.admin.master.bussiness.admin.domain.CompanyProviderBind;
import com.lysj.admin.master.bussiness.admin.service.CompanyProviderBindService;
import com.lysj.admin.master.bussiness.company.recharge.domain.CostRecord;
import com.lysj.admin.master.bussiness.company.invoice.domain.InvoiceApply;
import com.lysj.admin.master.bussiness.company.invoice.service.InvoiceApplyService;
import com.lysj.admin.master.common.exception.BaseException;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.bussiness.company.recharge.domain.RechargeRecord;
import com.lysj.admin.master.bussiness.company.recharge.mapper.RechargeRecordMapper;
import com.lysj.admin.master.common.web.Resp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;

/**
 * @author Created by drj on 2019-12-27 11:56
 * @description
 */
@Slf4j
@Service
@Transactional
public class RechargeRecordService extends MyService<RechargeRecordMapper, RechargeRecord> {


    @Resource
    private CostRecordService costRecordService;
    @Resource
    private CompanyProviderBindService companyProviderBindService;
    @Resource
    private InvoiceApplyService invoiceApplyService;


    public String audit(String id, Integer rechargeStatus) {

        RechargeRecord rechargeRecord = this.getById(id);
        if (!RechargeRecord.RechargeStatus.AUDITING.getCode().equals(rechargeRecord.getRechargeStatus())) {
            throw new BaseException(Resp.ResStatus.PARAM_ERROR, "充值记录非待审核状态,请勿重复操作");
        }
        rechargeRecord.setRechargeStatus(rechargeStatus);
        this.saveOrUpdate(rechargeRecord);
        //如果选择的是驳回
        if (RechargeRecord.RechargeStatus.REJECTED.getCode().equals(rechargeStatus)) {
            return "操作成功";
        }

        //创建充值费用明细
        CostRecord rechargeCostRecord = new CostRecord();
        rechargeCostRecord.setCompanyId(rechargeRecord.getCompanyId());
        rechargeCostRecord.setRechargeRecordId(rechargeRecord.getId());
        rechargeCostRecord.setServiceProviderName(rechargeRecord.getServiceProviderName());
        rechargeCostRecord.setOperationType(CostRecord.OperationType.INCOME.getCode());
        rechargeCostRecord.setAmount(rechargeRecord.getRechargePrice());
        rechargeCostRecord.setOperation(CostRecord.Operation.RECHARGE.getCode());
        rechargeCostRecord.setRemark("充值");
        costRecordService.save(rechargeCostRecord);

        //获取用人企业绑定服务商
        CompanyProviderBind companyProviderBind = companyProviderBindService.
                findByCompanyIdAndServiceProviderId(rechargeRecord.getCompanyId(), rechargeRecord.getServiceProviderId());
        //服务费率
        BigDecimal rate = BigDecimal.ONE.add(new BigDecimal("0.01").multiply(companyProviderBind.getFeeRate()));
        //计算实际到账金额(服务费率除以100)
        BigDecimal actPrice = rechargeRecord.getRechargePrice().divide(rate, 4, BigDecimal.ROUND_HALF_UP);
        //服务费
        BigDecimal commission = rechargeRecord.getRechargePrice().subtract(actPrice);
        /* 更改充值总额 */
        companyProviderBind.setTotalMoney(rechargeRecord.getRechargePrice());
        //更改服务费
        companyProviderBind.setCommission(commission);
        //更改账户余额
        companyProviderBind.setBalance(companyProviderBind.getBalance().add(actPrice));
        companyProviderBindService.saveOrUpdate(companyProviderBind);

        //创建服务费的费用明细
        CostRecord commissionCostRecord = new CostRecord();
        commissionCostRecord.setCompanyId(rechargeRecord.getCompanyId());
        commissionCostRecord.setRechargeRecordId(rechargeRecord.getId());
        commissionCostRecord.setServiceProviderName(rechargeRecord.getServiceProviderName());
        commissionCostRecord.setOperationType(CostRecord.OperationType.PAY.getCode());
        commissionCostRecord.setAmount(commission);
        commissionCostRecord.setOperation(CostRecord.Operation.COMMISSION.getCode());
        commissionCostRecord.setRemark("服务费");
        costRecordService.save(commissionCostRecord);

        //创建发票申请记录
        InvoiceApply invoiceApply = new InvoiceApply();
        invoiceApply.setRechargeRecordId(rechargeRecord.getId());
        invoiceApply.setCompanyId(rechargeRecord.getCompanyId());
        invoiceApply.setServiceProviderId(rechargeRecord.getServiceProviderId());
        invoiceApply.setServiceProviderName(rechargeRecord.getServiceProviderName());
        invoiceApply.setLeftMoney(rechargeRecord.getRechargePrice());
        invoiceApply.setServiceMoney(commission);
        invoiceApply.setBusinessMoney(actPrice);
        invoiceApplyService.save(invoiceApply);
        return "操作成功";
    }
}
